﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PSS4Publish.Common
{
    public static class EnumConverter<T> where T : new()
    {
        public static T ConvertToEnum(string enumString)
        {
            T objEnum = default(T);
            Type enumType = typeof(T);
            Array enumValues = Enum.GetValues(enumType);
            string[] enumNames = Enum.GetNames(enumType);

            for (int i = 0; i < enumValues.Length; i++)
            {
                if (string.Equals(Convert.ToInt32(enumValues.GetValue(i)).ToString(), enumString))
                {
                    objEnum = (T)Enum.Parse(typeof(T), enumString);
                    return objEnum;
                }
            }

            for (int i = 0; i < enumNames.Length; i++)
            {
                if (string.Equals(enumNames.GetValue(i).ToString(), enumString))
                {
                    objEnum = (T)Enum.Parse(typeof(T), enumString);
                    return objEnum;
                }
            }

            return objEnum;

        }
    }
}
